package com.zy.explore.license.domain.mybatis.entity;


import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.explore.license.infrastructure.base.mybatis.MpBaseEntity;
import com.zy.explore.license.infrastructure.util.StrUtil;
import com.zy.explore.license.interfaces.frm.ToDoItemFrm;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import javax.persistence.Id;
import java.util.Date;

/**
 * (表to_do_item)ORM实体对象
 *
 * @author ChinaYin  主页:https://gitee.com/zy-explorej2ee 邮箱:zyexplorej2ee@sina.com
 * @date 2021年06月13日 19:22:19
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("to_do_item")
public class ToDoItem extends MpBaseEntity<ToDoItem, Long> {

    /**
     * 主键ID
     */
    @Id
    @TableId
    private Long id;

    /**
     * 用户ID
     */
    private Integer userId;

    /**
     * 事项名称
     */
    private String itemName;

    /**
     * 事项内容
     */
    private String itemContent;

    /**
     * 计划完成时间
     */
    private Date planFinishTime;

    /**
     * 实际完成时间
     */
    private Date realFinishTime;

    /**
     * 完成状态：0.待办 1.已完成
     */
    private Integer finishStatus;

    /**
     * 删除状态：0.正常 1.已删除
     */
    private Integer delFlag;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * DTO转Example条件查询对象
     *
     * @param frm 查询参数对象
     * @return 返回Example对象
     */
    public static QueryWrapper<ToDoItem> getWrapper(ToDoItemFrm frm) {
        if (frm.getPageNum() == null || frm.getPageNum() <= 0) {
            frm.setPageNum(1);
        }
        if (frm.getPageSize() == null || frm.getPageSize() <= 0) {
            frm.setPageSize(10);
        }
        QueryWrapper<ToDoItem> wrapper = new QueryWrapper<>();
        if (frm.getId() != null) {
            wrapper.eq(StrUtil.fieldToColumn("id"), frm.getId());
        }
        if (frm.getUserId() != null) {

            // wrapper.eq(StrUtil.fieldToColumn("userId"), frm.getUserId());
            wrapper.eq(StrUtil.fieldToColumn("userId"), frm.getUserId());
        }
        if (!StringUtils.isEmpty(frm.getItemName())) {
            wrapper.like(StrUtil.fieldToColumn("itemName"), "%" + frm.getItemName() + "%");
        }
        if (!StringUtils.isEmpty(frm.getItemContent())) {
            wrapper.like(StrUtil.fieldToColumn("itemContent"), "%" + frm.getItemContent() + "%");
        }
        if (!StringUtils.isEmpty(frm.getPlanFinishTimeBegin())) {
            String planFinishTime = StrUtil.fieldToColumn("planFinishTime");
            wrapper.ge(planFinishTime, frm.getPlanFinishTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getPlanFinishTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("planFinishTime"), frm.getPlanFinishTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getRealFinishTimeBegin())) {
            wrapper.ge(StrUtil.fieldToColumn("realFinishTime"), frm.getRealFinishTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getRealFinishTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("realFinishTime"), frm.getRealFinishTimeEnd());
        }
        if (frm.getFinishStatus() != null) {
            wrapper.eq(StrUtil.fieldToColumn("finishStatus"), frm.getFinishStatus());
        }
        if (frm.getDelFlag() != null) {
            wrapper.eq(StrUtil.fieldToColumn("delFlag"), frm.getDelFlag());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeBegin())) {
            wrapper.ge(StrUtil.fieldToColumn("createTime"), frm.getCreateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getCreateTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("createTime"), frm.getCreateTimeEnd());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeBegin())) {
            wrapper.ge(StrUtil.fieldToColumn("updateTime"), frm.getUpdateTimeBegin());
        }
        if (!StringUtils.isEmpty(frm.getUpdateTimeEnd())) {
            wrapper.le(StrUtil.fieldToColumn("updateTime"), frm.getUpdateTimeEnd());
        }
        return wrapper;
    }

}